define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'volunteer_data/index' + location.search,
                    add_url: 'volunteer_data/add',
                    edit_url: 'volunteer_data/edit',
                    del_url: 'volunteer_data/del',
                    multi_url: 'volunteer_data/multi',
                    import_url: 'volunteer_data/import',
                    table: 'volunteer',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormTemplate: 'customformtpl',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                // showExport:false,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                exportOptions: {
                    fileName: '義工列表_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: ['user.avatar','operate'], //不导出字段
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('義工Id')},
                        {field: 'affiliated_center_text', title: __('所屬中心'), operate:false, formatter: Table.api.formatter.label},
                        {field: 'user.avatar', title: __('用戶頭像'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                     
                        {field: 'name', title: __('姓名'), operate: 'LIKE'},
                        {field: 'mobile', title: __('手機號'), operate: 'LIKE'},
                      
                        {field: 'workinghours', title: __('義工時數')},
                         {field: 'createtime', title: __('加入時間'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                         {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter:function(value,row,index){
                            this.buttons = [
                             
                               
                                   {
                                    name: 'area_staff',
                                    text: '時數明細',
                                    classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                    url: 'activity/sign',
                                    // confirm: '确认禁用嗎？',
                                    visible: function (row) {
                                        // if ( row.level == 2 &&  Config.area_staff) {
                                        //     return true;
                                        // }else{
                                        //     return false;
                                        // }
                                        return true;
                                        //返回true时按钮显示,返回false隐藏
                                    },
                                    success: function () {
                                        $(".btn-refresh").trigger("click");
                                    },
                                    error: function () {

                                    },
                                    refresh: true,
                                }
                                ];
                                return Table.api.formatter.buttons.call(this,value,row,index)
                            }
                        }
                    ]
                ]
            });

            table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        // 區域義工列表
        area_volunteer: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'volunteer_data/area_volunteer_data/ids/'+Config.ids + location.search,
                    // add_url: 'volunteer_data/add',
                    // edit_url: 'volunteer_data/edit',
                    // del_url: 'volunteer_data/del',
                    multi_url: 'volunteer_data/multi',
                    import_url: 'volunteer_data/import',
                    table: 'volunteer',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: false,
                search:false,
                showColumns:false,
                showExport:false,
                commonSearch:false,
                pageSize: 10,
                showToggle: false,
                exportOptions: {
                    fileName: '義工列表_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: ['user.avatar','operate'], //不导出字段
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.id', title: '用戶id'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'area', title: __('Region_address'), operate: false},
                        {field: 'training_staff', title: __('Training_staff'), operate: false},
                        {field: 'emergency_contact', title: __('Emergency_contact'), operate: false},
                        {field: 'volunteerspecialty.name', title: __('Volunteerspecialty.name'), operate: 'LIKE'},
                        {field: 'service_time_type', title: __('Service_time_type'), searchList: {"1":__('Service_time_type 1'),"2":__('Service_time_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'staff.nickname', title: '所屬員工信息', operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        indexs: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'volunteer_data/indexs/source/'+Config.source+'/staff_id/'+Config.staff_id + location.search,
                    add_url: 'volunteer_data/add',
                    edit_url: 'volunteer_data/edit',
                    del_url: 'volunteer_data/del',
                    multi_url: 'volunteer_data/multi',
                    import_url: 'volunteer_data/import',
                    table: 'volunteer',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:false,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                maintainSelected: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'volunteerspecialty.name', title: __('Volunteerspecialty.name'), operate: false},
                        {field: 'service_time_type', title: __('Service_time_type'), operate: false, searchList: {"1":__('Service_time_type 1'),"2":__('Service_time_type 2')}, formatter: Table.api.formatter.normal},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function () {
                // 默認選中
                $("#table").bootstrapTable("checkBy", {field:"id", values:Config.arr})
            });
            // 获取选中项
            $(document).on("click", ".btn-selected", function () {
                // data = JSON.stringify(Table.api.selecteddata(table));
                data = Table.api.selecteddata(table);
                // Layer.alert();
                Fast.api.close(data);//这里是重点
            });
        },
        indexss: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'volunteer_data/indexss/ids/'+Config.ids + location.search,
                    add_url: 'volunteer_data/add',
                    edit_url: 'volunteer_data/edit',
                    del_url: 'volunteer_data/del',
                    multi_url: 'volunteer_data/multi',
                    import_url: 'volunteer_data/import',
                    table: 'volunteer',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:false,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                maintainSelected: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'volunteerspecialty.name', title: __('Volunteerspecialty.name'), operate: false},
                        {field: 'service_time_type', title: __('Service_time_type'), operate: false, searchList: {"1":__('Service_time_type 1'),"2":__('Service_time_type 2')}, formatter: Table.api.formatter.normal},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function () {
                // 默認選中
                $("#table").bootstrapTable("checkBy", {field:"id", values:Config.arr})
            });
            // 启动和暂停按钮
            $(document).on("click", ".btn-start", function () {
                //在table外不可以使用添加.btn-change的方法
                //只能自己调用Table.api.multi实现
                //如果操作全部则ids可以置为空
                var ids = Table.api.selectedids(table);
                Fast.api.ajax({
                 url:$('.btn-start').attr('data-url'),
                 data:{volunteer_ids:ids.join(","),action:'select'},
                loading:false,
                 success: function (data, ret) {
                    if ( data.code == 1 ) {
                        Toastr.success(data.msg);
                        setTimeout('Fast.api.close()', 1500);
                    }else{

                      Toastr.error(data.msg);
                     //这里ret.msg 是未定义的。 data里的数据需要 data.data.*来使用
                    }
                 }
                 });
                // Table.api.multi("", ids.join(","), table, this);
                // Fast.api.close();//这里是重点



                // Layer.close('操作成功');
            });

        },
        indexsss: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'volunteer_data/indexsss/ids/'+Config.ids + location.search,
                    add_url: 'volunteer_data/add',
                    edit_url: 'volunteer_data/edit',
                    del_url: 'volunteer_data/del',
                    multi_url: 'volunteer_data/multi',
                    import_url: 'volunteer_data/import',
                    table: 'volunteer',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:false,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                maintainSelected: true,
                singleSelect: true, //是否启用单选
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'volunteerspecialty.name', title: __('Volunteerspecialty.name'), operate: false},
                        {field: 'service_time_type', title: __('Service_time_type'), operate: false, searchList: {"1":__('Service_time_type 1'),"2":__('Service_time_type 2')}, formatter: Table.api.formatter.normal},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function () {
                // 默認選中
                $("#table").bootstrapTable("checkBy", {field:"id", values:Config.arr})
            });
            // 启动和暂停按钮
            $(document).on("click", ".btn-start", function () {
                //在table外不可以使用添加.btn-change的方法
                //只能自己调用Table.api.multi实现
                //如果操作全部则ids可以置为空
                var ids = Table.api.selectedids(table);
                if ( !ids.join(",") ) {
                      Toastr.error('請選擇一項');
                      return false;
                }
                Fast.api.ajax({
                url:$('.btn-start').attr('data-url'),
                data:{volunteer_id:ids.join(","),action:'select'},
                loading:false,
                 success: function (data, ret) {
                    if ( data.code == 1 ) {
                        Toastr.success(data.msg);
                        setTimeout('Fast.api.close()', 1500);
                    }else{

                      Toastr.error(data.msg);
                     //这里ret.msg 是未定义的。 data里的数据需要 data.data.*来使用
                    }
                 }
                 });
                // Table.api.multi("", ids.join(","), table, this);
                // Fast.api.close();//这里是重点



                // Layer.close('操作成功');
            });

        },
        staff_volunteers: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'volunteer_data/staff_volunteers/ids/'+Config.ids + location.search,
                    add_url: 'volunteer_data/add',
                    edit_url: 'volunteer_data/edit',
                    del_url: 'volunteer_data/del',
                    multi_url: 'volunteer_data/multi',
                    import_url: 'volunteer_data/import',
                    table: 'volunteer',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:false,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                maintainSelected: true,
                columns: [
                    [
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'user.avatar', title: __('User.avatar'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'volunteerspecialty.name', title: __('Volunteerspecialty.name'), operate: false},
                        {field: 'service_time_type', title: __('Service_time_type'), operate: false, searchList: {"1":__('Service_time_type 1'),"2":__('Service_time_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'region_address', title: __('Region_address'), operate: false},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function () {
                // 默認選中
                $("#table").bootstrapTable("checkBy", {field:"id", values:Config.arr})
            });
            // 获取选中项
            $(document).on("click", ".btn-selected", function () {
                // data = JSON.stringify(Table.api.selecteddata(table));
                data = Table.api.selecteddata(table);
                // Layer.alert();
                Fast.api.close(data);//这里是重点
            });
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'volunteer_data/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'volunteer_data/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'volunteer_data/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});